package org.iteam.xlz.core.service.user;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.user.UserAddress;

public class UserAddressService extends BaseService<UserAddress>{
	static UserAddressService instance;
	
	private List<Object> params=new ArrayList<Object>();
	private StringBuilder sql=null;
	private UserAddressService(){}
	
	public static UserAddressService getInstance(){
		if(null==instance){
			instance=new UserAddressService();
		}
		return instance;
	}
	
	public long counts(String uid){
		params.clear();
		sql=new StringBuilder("select count(1) from useraddress where 1=1");
		if(StringUtils.isNotBlank(uid)){
			sql.append(" and uid=?");
			params.add(uid);
		}
		try {
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<UserAddress> query(String uid,long page,long rows){
		params.clear();
		sql=new StringBuilder("select * from useraddress where 1=1");
		if(StringUtils.isNotBlank(uid)){
			sql.append(" and uid=?");
			params.add(uid);
		}
		sql.append(" order by createtime desc limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		try{
			return super.query(sql.toString(),params.toArray());
		}catch(SQLException e){
			e.printStackTrace();
			return new ArrayList<UserAddress>();
		}
	}
}
